package top.whysu.edevice.websocket.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import top.whysu.edevice.dto.websocket.WebSocketPushBaseDTO;
import top.whysu.edevice.websocket.WebsocketService;

/**
 * websocket消息推送业务
 */
@Service("websocketService")
public class WebsocketServiceImpl implements WebsocketService {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Override
    public void sendToUsers(String destination, Integer method, Object params) {
        WebSocketPushBaseDTO webSocketPushBaseDTO = new WebSocketPushBaseDTO();
        webSocketPushBaseDTO.setMethod(method);
        webSocketPushBaseDTO.setParams(params);
        messagingTemplate.convertAndSend(destination, webSocketPushBaseDTO);
    }

    @Override
    public void sendToUser(String user, String destination, Integer method, Object params) {
        WebSocketPushBaseDTO webSocketPushBaseDTO = new WebSocketPushBaseDTO();
        webSocketPushBaseDTO.setMethod(method);
        webSocketPushBaseDTO.setParams(params);
        messagingTemplate.convertAndSendToUser(user, destination, webSocketPushBaseDTO);
    }
}
